package com.dehui.traders.ui.market.plate;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.bean.PlateListBean;
import com.dehui.traders.databinding.FragmentPlateMarketBinding;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by hqq on 18.4.2.
 */

public class PlateMarketFragment extends BaseFragment<FragmentPlateMarketBinding,PlateViewModel>{

    private PlateViewModel viewModel;
    private List<PlateListBean> mList = new ArrayList<>();
    private PlateAdapter adapter;
    private SmartRefreshLayout refreshLayout;

    @Override
    public int initContentView() {
        return R.layout.fragment_plate_market;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public PlateViewModel initViewModel() {
        viewModel = new PlateViewModel(getContext(),this);
        return viewModel;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        refreshLayout = view.findViewById(R.id.refresh_view);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PlateAdapter(mList,getActivity());
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                viewModel.tradeupdownList("",0);
            }
        });
        viewModel.tradeupdownList("",0);
    }

    public void initList(List<PlateListBean> list) {
        refreshLayout.finishRefresh();
        mList = list;
        adapter.refresh(mList);
        adapter.notifyDataSetChanged();
    }

    public void onFailuer() {
        refreshLayout.finishRefresh();
    }
}
